package com.fzy.silntgym.weight.MSfab.models;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.text.format.DateFormat;

import com.fzy.silntgym.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;

/**
 * Created by 77dfeba on 2016/9/9.
 */

public class FavoritesNote {
    public static final String NOTICE1 = "欢迎使用无声广场舞！";
    public static final String WORD1 = "您可以在【广场讨论】中与大家一起讨论话题，还可以在【教学视屏】中下载教学视频，下载完成后可以在【一起跳】里面的【目录】选择观看。如果遇到问题您可以直接咨询300-219-XXX，感谢您的使用";
    public static final String NOTICE2 = "系统维护通告";
    public static final String WORD2 = "系统数据库将进行维护，软件网络功能无法正常使用，请悉知！";
    private static final String[] ACTIONS_PEOPLE = { "call", "email", "meet up with",
            "hang out with" };
    private static final String[] ACTIONS_OBJECTS = { "clean", "buy", "sell", "fix" };
    private static final String[] NAMES = { "Sherry", "Gordon", "Tom", "Kevin", "Brian", "Naomi",
            "Ali", "Jennifer" };
    private static final String[] OBJECTS = { "desk", "car", "motorcycle", "computer", "laptop" };
    private static final String WORDS = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
    private static final String[] CITIES = { "San Francisco", "Campbell", "Lincoln", "New York",
            "Silverton", "Scarface", "King Salmon" };

    private static final String[] LIST_TITLES = { "shopping", "to bring", "on sale", "look for",
            "buy", "get rid of" };
    private static final String[] LIST_DELIMITERS = { "•", "-" };
    private static final String[] LIST_GROCERIES = { "almond milk", "coconut water", "cucumber",
            "green apples" };
    private static final String[] LIST_CAMPING = { "lantern", "smores", "extra blankets",
            "warm socks", "first aid kit", "tent" };

    private static final int NUM_WORDS = 4;
    private static final int DATE_RANGE = -10;

    private String title;
    private String note;
    private String info;
    @DrawableRes
    private int infoImage;
    private int color;
    private static int isnotice = 0;

    private FavoritesNote(String title, String note, String info, int infoImage, int color) {
        this.title = title;
        this.note = note;
        this.info = info;
        this.infoImage = infoImage;
        this.color = color;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public int getInfoImage() {
        return infoImage;
    }

    public void setInfoImage(int infoImage) {
        this.infoImage = infoImage;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public static FavoritesNote randomNote(Context context) {
        String title = "";
        String note = "";
        NoteInfo info = new NoteInfo("", 0);
        int color = getRandomColor(context);
        title = getRandomActivity(isnotice);
        note = getRandomWords(isnotice);
        info = getRandomInfo(context);

        return new FavoritesNote(capitalize(title), note, info.info, info.infoImage, color);
    }

    private static String getRandomActivity(int isnotice) {
        if(isnotice <=1){
            FavoritesNote.isnotice++;
            return NOTICE1;
        }
            return NOTICE2;
    }

    private static String getRandomWords(int isnotice) {
        if(isnotice <=1){
            FavoritesNote.isnotice++;
            return WORD1;
        }
            return WORD2;
    }

    private static String getRandomListTitle() {
        String title = getRandomString(true, LIST_TITLES);
        if (!TextUtils.isEmpty(title)) {
            title += ":";
        }
        return title;
    }

    private static String getRandomList() {
        String[] list = (String[]) getRandomItem(new Object[] { LIST_GROCERIES, LIST_CAMPING });
        String delimiter = getRandomString(true, LIST_DELIMITERS);
        if (!TextUtils.isEmpty(delimiter)) {
            delimiter += " ";
        }
        String listStr = "";
        for (int i = 0; i < list.length; i++) {
            listStr += delimiter + list[i];
            if (i != list.length - 1) {
                listStr += "\n";
            }
        }
        return listStr;
    }

    private static FavoritesNote.NoteInfo getRandomInfo(Context context) {
        FavoritesNote.NoteInfo[] infos = new FavoritesNote.NoteInfo[] { getRandomDate(context) };
        return (FavoritesNote.NoteInfo) getRandomItem(infos);
    }

    private static FavoritesNote.NoteInfo getRandomDate(Context context) {
        Calendar cal = Calendar.getInstance();
        if(isnotice <=1)
            cal.add(Calendar.DATE, (int) (Math.random() * -20));
        else
        cal.add(Calendar.DATE, (int) (Math.random() * DATE_RANGE));
        String date = DateFormat.getMediumDateFormat(context).format(cal.getTime());
        return new FavoritesNote.NoteInfo(date, R.drawable.ic_event_white_24dp);
    }
    //get city
    private static FavoritesNote.NoteInfo getRandomLocation() {
        String location = getRandomString(false, CITIES);
        return new FavoritesNote.NoteInfo(location, R.drawable.ic_place_white_24dp);
    }

    private static int getRandomColor(Context context) {
        int[] colors;
        if (Math.random() >= 0.6) {
            colors = context.getResources().getIntArray(R.array.note_accent_colors);
        } else {
            colors = context.getResources().getIntArray(R.array.note_neutral_colors);
        }
        return colors[((int) (Math.random() * colors.length))];
    }

    private static String capitalize(String str) {
        if (TextUtils.isEmpty(str)) {
            return str;
        }
        return Character.toUpperCase(str.charAt(0)) + str.substring(1);
    }

    private static String getRandomString(boolean includeEmpty, String[] strings) {
        if (includeEmpty) {
            ArrayList<String> stringsWithEmpty = new ArrayList<>(Arrays.asList(strings));
            stringsWithEmpty.add("");
            return (String) getRandomItem(stringsWithEmpty.toArray());
        }
        return (String) getRandomItem(strings);
    }

    private static Object getRandomItem(Object[] objs) {
        return objs[((int) (Math.random() * objs.length))];
    }

    private static class NoteInfo {

        private String info;
        private int infoImage;

        private NoteInfo(String info, int infoImage) {
            this.info = info;
            this.infoImage = infoImage;
        }

    }
}
